<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="nuxt 打包文件hash问题来源 因为项目要读取图片并进行显示，然后，我发现可以在本地运行，但是却在打包之后图片没有显示，仔细一看，人家的打包文件带了hash名，所以你的名字不能命中了 为什么需要hash既然知道了文件名被hash了，那么我们进一步思考，为什么需要进行hash呢， 对文件名进行一个hash，可以说是一个非常机智的一个方法，因为我们为了能够在网络的传输可以快点，所以我们就想了一个办">
<meta property="og:type" content="article">
<meta property="og:title" content="nuxt打包文件hash">
<meta property="og:url" content="http://tallgy.gitee.io/2022/01/12/%E9%9A%8F%E7%AC%94/nuxt%E6%89%93%E5%8C%85%E6%96%87%E4%BB%B6hash/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="nuxt 打包文件hash问题来源 因为项目要读取图片并进行显示，然后，我发现可以在本地运行，但是却在打包之后图片没有显示，仔细一看，人家的打包文件带了hash名，所以你的名字不能命中了 为什么需要hash既然知道了文件名被hash了，那么我们进一步思考，为什么需要进行hash呢， 对文件名进行一个hash，可以说是一个非常机智的一个方法，因为我们为了能够在网络的传输可以快点，所以我们就想了一个办">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-01-12T09:56:45.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.338Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="webpack">
<meta property="article:tag" content="文件hash">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://tallgy.gitee.io/2022/01/12/%E9%9A%8F%E7%AC%94/nuxt%E6%89%93%E5%8C%85%E6%96%87%E4%BB%B6hash/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2022/01/12/%E9%9A%8F%E7%AC%94/nuxt%E6%89%93%E5%8C%85%E6%96%87%E4%BB%B6hash/","path":"2022/01/12/随笔/nuxt打包文件hash/","title":"nuxt打包文件hash"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>nuxt打包文件hash | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#nuxt-%E6%89%93%E5%8C%85%E6%96%87%E4%BB%B6hash"><span class="nav-number">1.</span> <span class="nav-text">nuxt 打包文件hash</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81hash"><span class="nav-number">1.1.</span> <span class="nav-text">为什么需要hash</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2022/01/12/%E9%9A%8F%E7%AC%94/nuxt%E6%89%93%E5%8C%85%E6%96%87%E4%BB%B6hash/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          nuxt打包文件hash
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-01-12 17:56:45" itemprop="dateCreated datePublished" datetime="2022-01-12T17:56:45+08:00">2022-01-12</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="nuxt-打包文件hash"><a href="#nuxt-打包文件hash" class="headerlink" title="nuxt 打包文件hash"></a>nuxt 打包文件hash</h1><p>问题来源</p>
<p>因为项目要读取图片并进行显示，然后，我发现可以在本地运行，但是却在打包之后图片没有显示，仔细一看，人家的打包文件带了hash名，所以你的名字不能命中了</p>
<h2 id="为什么需要hash"><a href="#为什么需要hash" class="headerlink" title="为什么需要hash"></a>为什么需要hash</h2><p>既然知道了文件名被hash了，那么我们进一步思考，为什么需要进行hash呢，</p>
<p>对文件名进行一个hash，可以说是一个非常机智的一个方法，因为我们为了能够在网络的传输可以快点，所以我们就想了一个办法，那就是使用缓存，说到缓存，那就不得不说强缓存和协商缓存了。</p>
<p>​        强缓存会直接在文件没有过时的时候不用向服务器进行请求，便可以得到资源，可以减少很多网络的传输，</p>
<p>​        协商缓存会在将文件的etag和last-modified发给服务器，服务器通过比对这两个值来判断文件是否被修改，只要文件没有被修改，那么客户端就会使用本地的资源，这样虽然进行了请求，但是却减少了网络的资源传输</p>
<p>知道了强缓存和协商缓存，那么，什么是hash呢？</p>
<p>​        在此之前，我们先思考一个问题，对于一个项目，在你上线了之后，出现了bug，需要进行更改，但是此时对应的文件处于强缓存阶段，那么如何才能判断文件是否被修改了，此时就需要使用hash了，因为对于文件发生了更改，hash的名字也会对应发生改变，然后此时就获取到了新的文件。</p>
<p>说完之后，就是解决 nuxt默认配置的hash了</p>
<p>​        这个其实在 nuxt 官网就已经说完了，我们如果想要取消hash，也只需要将 contenthash:7 改为 name 就会变成名字，而不会携带7位hash了。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://www.nuxtjs.cn/api/configuration-build#filenames</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/webpack/" rel="tag"># webpack</a>
              <a href="/tags/%E6%96%87%E4%BB%B6hash/" rel="tag"># 文件hash</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/01/11/%E9%9A%8F%E7%AC%94/other/jet-brains%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9%E7%9A%84%E6%AD%A3%E5%88%99%E6%9F%A5%E6%89%BE%E5%92%8C%E6%9B%BF%E6%8D%A2/" rel="prev" title="jet-brains文件内容的正则查找和替换">
                  <i class="fa fa-chevron-left"></i> jet-brains文件内容的正则查找和替换
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/01/19/JavaScript/%E9%9A%8F%E7%AC%94/js%E9%81%8D%E5%8E%86%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E6%B3%95/" rel="next" title="js遍历对象的方法">
                  js遍历对象的方法 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
